import os

from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget

from ..config import ExcelConfig, R
from ..core.excel import ExcelTask
from .gen_tasks_ui import Ui_Form


class GenTasksWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton_SelectOrgFile.clicked.connect(self.SelectOrgFile)
        self.ui.pushButton_SelectDestFile.clicked.connect(self.SelectDestFile)
        self.ui.pushButton_gen_task.clicked.connect(self.Gen)
        self.InitUi()
        self.filename = '.'

    def InitUi(self):
        self.ui.lineEdit_OrgFilename.setText(R.T_OrgFilename)
        self.ui.lineEdit_OrgSheetName.setText(R.T_OrgSheetName)
        self.ui.lineEdit_OrgSheetStartRow.setText(R.T_OrgSheetStartRow)
        self.ui.lineEdit_OrgSheetEndRow.setText(R.T_OrgSheetEndRow)
        self.ui.lineEdit_DestFilename.setText(R.T_TempFilename)
        self.ui.lineEdit_DestSheetName.setText(R.T_TempSheetname)
        self.ui.radioButton_gen_task.setChecked(R.T_IsTask)
        self.ui.radioButton_gen_students.setChecked(not R.T_IsTask)

    def SelectOrgFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_OrgFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def SelectDestFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_DestFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def Gen(self):
        if self.ui.radioButton_gen_task.isChecked():
            self.GenTask()
        elif self.ui.radioButton_gen_students.isChecked():
            self.GenStudents()

    def GenTask(self):
        """
        生成任务责任书，格式有变，需改代码
        """
        try:
            org_filename = self.ui.lineEdit_OrgFilename.text()
            org_sheetname = self.ui.lineEdit_OrgSheetName.text()
            org_startRow = int(self.ui.lineEdit_OrgSheetStartRow.text())
            org_endRow = int(self.ui.lineEdit_OrgSheetEndRow.text())
            dest_filename = self.ui.lineEdit_DestFilename.text()
            dest_sheetname = self.ui.lineEdit_DestSheetName.text()

            exl = ExcelTask(
                src_path=org_filename,
                src_sheetname=org_sheetname,
                src_start_row=org_startRow,
                src_end_row=org_endRow,
                dest_path=dest_filename,
                dest_sheetname=dest_sheetname,
            )
            exl.FillTask()

            QMessageBox.about(self, R.AppName, "生成任务成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")

    def GenStudents(self):
        self.setWindowTitle("正在运行,请等待...")
        """
        生成学生导入表，格式有变，需改代码
        """
        try:
            org_filename = self.ui.lineEdit_OrgFilename.text()
            org_sheetname = self.ui.lineEdit_OrgSheetName.text()
            org_startRow = int(self.ui.lineEdit_OrgSheetStartRow.text())
            org_endRow = int(self.ui.lineEdit_OrgSheetEndRow.text())
            dest_filename = self.ui.lineEdit_DestFilename.text()
            dest_sheetname = self.ui.lineEdit_DestSheetName.text()

            exl = ExcelTask(
                src_path=org_filename,
                src_sheetname=org_sheetname,
                src_start_row=org_startRow,
                src_end_row=org_endRow,
                dest_path=dest_filename,
                dest_sheetname=dest_sheetname,
            )
            exl.GenerateDetails()

            QMessageBox.about(self, R.AppName, "生成学生导入模板成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"Error: in GetStudents(), 请填充正确的数字！Errors: {e}")
        self.setWindowTitle(R.AppName)

